package cn.tedu.fy.pethome.controller;

import cn.tedu.fy.pethome.pojo.dto.OrderAddDTO;
import cn.tedu.fy.pethome.pojo.dto.OrderListTimeDTO;
import cn.tedu.fy.pethome.pojo.dto.OrderStateUpdateDTO;
import cn.tedu.fy.pethome.pojo.entity.OrderItem;
import cn.tedu.fy.pethome.pojo.restful.JsonPage;
import cn.tedu.fy.pethome.pojo.vo.OrderAddVO;
import cn.tedu.fy.pethome.pojo.vo.OrderListVO;
import cn.tedu.fy.pethome.service.IOrderService;
import cn.tedu.fy.pethome.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fy/order")
@Api(tags="订单功能")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    @ApiOperation("新增订单的方法")
    @PreAuthorize("hasRole('user')")
    public JsonResult<OrderAddVO> addOrder(@Validated OrderAddDTO orderAddDTO){
        OrderAddVO orderAddVO=orderService.addOrder(orderAddDTO);
        return JsonResult.ok(orderAddVO);
    }
    // 查询订单的方法
    @GetMapping("/list")
    @ApiOperation("分页查询当前用户指定时间范围订单")
    @PreAuthorize("hasRole('user')")
    public JsonResult<List<OrderListVO>> listUserOrders(OrderListTimeDTO orderListTimeDTO){
        List<OrderListVO> list=orderService.listOrders(orderListTimeDTO);
        return JsonResult.ok(list);
    }

    // 修改订单状态的方法
    @GetMapping("/update/state/{id}")
    @ApiOperation("修改订单状态的方法")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult updateOrderState(@PathVariable Long id){
        orderService.updateOrderState(id);
        return JsonResult.ok();
    }
    //根据orderId查询所有orderItem的信息
    @GetMapping("/list/orderItem/{orderId}")
    @ApiOperation("根据orderId查询所有orderItem的信息")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult selectOrderItems(@PathVariable Long orderId){
        System.out.println(orderId);
        List<OrderItem> orderItems = orderService.getOrderItems(orderId);
        return JsonResult.ok(orderItems);
    }

    //根据spuId删除订单以及订单下的子项订单
    @GetMapping("/delete/{id}")
    @ApiOperation("根据orderId查询所有orderItem的信息")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult deleteOrderAndOrderItems(@PathVariable Long id){
        System.out.println(id);
        orderService.deleteOrderAndOrderItems(id);
        return JsonResult.ok();
    }
}
